// pages/shop/zan.js
var app = getApp();
var currentProductId;
Page({
  data: {
    zan_status_str: ['未发起','已生效','已关闭']
  },
  onLoad: function (options) {
    currentProductId = options.id;
    var that = this;
    var shop = wx.getStorageSync('shop');
    app.func.req('products/show', {
      id: currentProductId,
      shop_id: shop.id
    }, 'GET', function (res) {
      var zan_no = res.result.zan_no;
      var zan_no_array = [];
      if(zan_no != null){
        var zan_no_array = zan_no.split(',');
      }
      var start = res.result.zan_started_at.split(' ');
      var end = res.result.zan_ended_at.split(' ');
      that.setData({
        product: res.result,
        start: start[0],
        startTime: start[0] == '0000-00-00' ? null :start[1],
        end: end[0],
        endTime: end[0] == '0000-00-00' ? null : end[1],
        zan_no_array: zan_no_array
      })
    }, {
        token: wx.getStorageSync('token')
      });
  },
  bindStart: function (e) {
    this.setData({
      start: e.detail.value
    })
  },
  bindStartTime: function (e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  bindEnd: function (e) {
    this.setData({
      end: e.detail.value
    })
  },
  bindEndTime: function (e) {
    this.setData({
      endTime: e.detail.value
    })
  },
  inputZanNo: function(e){
    this.setData({
      curZanNo: e.detail.value.trim()
    })
  },
  addZanNo: function(){
    var zan_no_array = this.data.zan_no_array;
    var curZanNo = this.data.curZanNo;
    if(curZanNo == ''||curZanNo == null){
      wx.showToast({
        title: '名次不能为空',
        image: '/images/fail.png',
        duration: 2000,
        mask: true
      });
      return;
    }
    for (var i = 0; i < zan_no_array.length;i++){
      if(zan_no_array[i] == curZanNo){
        wx.showToast({
          title: '名次已存在',
          image: '/images/fail.png',
          duration: 2000,
          mask: true
        });
        return;
      }
    }
    zan_no_array.push(curZanNo);
    this.setData({
      zan_no_array: zan_no_array,
      curZanNo:null
    })
  },
  delZanNo: function(e){
    var index = e.target.dataset.index;
    var that = this;
    var zan_no_array = this.data.zan_no_array;
    wx.showModal({
      title: '提示',
      content: '是否删除排序为' + zan_no_array[index]+'的中奖名次',
      success: function (res) {
        if (res.confirm) {
          zan_no_array.splice(index,1);
          that.setData({
            zan_no_array: zan_no_array
          })
        }
      }
    })
  },
  save: function(e){
    var that = this;
    var zan_price = e.detail.value.zan_price.trim();
    var zan_no_array = this.data.zan_no_array;
    var zan_started_at = this.data.start;
    var zan_ended_at = this.data.end;
    var zan_started_time = this.data.startTime;
    var zan_ended_time = this.data.endTime;

    zan_started_at =  zan_started_at + ' ' + zan_started_time;
    zan_ended_at =  zan_ended_at + ' ' + zan_ended_time

    var zan_no = zan_no_array.join();
    if(zan_price==''){
      wx.showToast({
        title: '点赞价不能为空',
        image: '/images/fail.png',
        duration: 2000,
        mask: true
      });
      return;
    } else if (zan_started_at == '0000-00-00 00:00' || zan_ended_at == '0000-00-00 00:00') {
      wx.showToast({
        title: '开始或结束时间未设置',
        image: '/images/fail.png',
        duration: 2000,
        mask: true
      });
      return;
    }
    app.func.req('products/update',{
      zan_price: zan_price,
      zan_no: zan_no,
      zan_started_at: zan_started_at,
      zan_ended_at: zan_ended_at,
      id: currentProductId,
      zan_status: 1
    }, 'POST', function (res) {
      console.log(res)
      if (res.code == 'success'){
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
        var product = that.data.product;
        product.zan_status = 1;
        that.setData({
          product: product
        })
        // wx.showLoading({ title: '点赞海报生成中...', mask: true });
        // app.func.req('products/qrcode', {
        //   id: currentProductId
        // }, 'POST', function (res) {
        //   console.log(res);
        //   if (res.code == "success") {
        //     wx.hideLoading();
        //     wx.previewImage({
        //       urls: [res.result],
        //     })
        //   }
        // }, {
        //     'token': wx.getStorageSync('token')
        //   })
      }
    }, {
        'token': wx.getStorageSync('token')
      });
  },
  doPoster: function(){
    wx.showLoading({ title: '点赞海报生成中...', mask: true });
    app.func.req('products/qrcode', {
      id: currentProductId,
      is_zan: true
    }, 'POST', function (res) {
      if (res.code == "success") {
        wx.hideLoading();
        wx.previewImage({
          urls: [res.result],
        })
      }
    }, {
        'token': wx.getStorageSync('token')
      })
  },
  closeZan: function(){
    var that = this;
    app.func.req('products/update', {
      id: currentProductId,
      zan_status: 2
    }, 'POST', function (res) {
      console.log(res)
      if (res.code == 'success') {
        wx.showToast({
          title: '关闭成功',
          icon: 'success',
          duration: 2000
        })
        var product = that.data.product;
        product.zan_status = 2;
        that.setData({
          product: product
        })
      }
    }, {
        'token': wx.getStorageSync('token')
      });
  }
})
